package com.gensdai.leliang.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gensdai.leliang.R;
import com.gensdai.leliang.base.BaseActivityNoAbs;
import com.gensdai.leliang.dialog.Upload;
import com.gensdai.leliang.entity.UserNameBean;
import com.gensdai.leliang.json_base.json_base;
import com.gensdai.leliang.remoteInterface.ApiService;
import com.gensdai.leliang.utils.BaseUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/5/27.
 */

public class set_login_mm extends BaseActivityNoAbs implements View.OnClickListener {
    @BindView(R.id.next_wave)
    TextView next_wave;
    @BindView(R.id.ui_title)
    TextView ui_title;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.one_mm)
    EditText oneMm;
    @BindView(R.id.two_mm)
    EditText twoMm;

    private Upload up;
    UserNameBean user;
    SharedPreferences share;
    SharedPreferences.Editor ed;

    private final int ENTER = 1;// 检验验证码
    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case ENTER:
                    up.dismiss();
                    try {
                        if (new json_base().GOOD((String) msg.obj)) {
                            set_login_mm.this.finish();
                        }
                        Toast.makeText(
                                set_login_mm.this,
                                new JSONObject((String) msg.obj)
                                        .getString("message"),
                                Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_login_mm);
        ButterKnife.bind(this);
        init();
        setonclick();
    }

    /**
     * 设置事件
     */
    private void setonclick() {
        next_wave.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    /**
     * 初始化
     */
    private void init() {
        ui_title.setText(getResources().getString(R.string.set_login_mm));
        share = getSharedPreferences("User", MODE_PRIVATE);
        up = new Upload(this);
        ed = share.edit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_wave:
                if (oneMm.getText().toString().trim().length() < 6) {
                    Toast.makeText(this, "密码不得小于6位!~", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!oneMm.getText().toString().trim().equals(twoMm.getText().toString().trim())) {
                    Toast.makeText(this, "两次密码不一致~", Toast.LENGTH_SHORT).show();
                    return;
                }
                forgetLoginPassword();
//                set_login_mm.this.finish();
                break;
            case R.id.back:
                set_login_mm.this.finish();
                break;
            default:
                break;
        }
    }

    /**
     * 检验验证码
     */
    private void forgetLoginPassword() {
        //获取edit的值后请求
        Map<String, String> loginParams = new HashMap<>();
        loginParams.put("phone", getIntent().getStringExtra("phone")); //手机号
        loginParams.put("firstPassword", BaseUtils.EncryptedPassword(oneMm.getText().toString().trim())); //第一遍密码
        loginParams.put("secoddPassword", BaseUtils.EncryptedPassword(twoMm.getText().toString().trim())); //第二遍密码
        toStr(loginParams);
    }

    //返回字符串类型
    private void toStr(Map<String, String> loginParams) {
        up.show();
        ApiService.getInstance(this).forgetLoginPassword(loginParams)
                //涉及到IO操作
                .subscribeOn(Schedulers.io())
                .compose(this.<String>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }

                        Log.d("accept", "accept");
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        //可以取消订阅
                        //还可以判断是否处于取消状态
                        //boolean b=d.isDisposed();

                        Log.d("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        //处理得到的数据
                        Log.e("quick_login", s);
                        Message msg = new Message();
                        msg.what = ENTER;
                        msg.obj = s;
                        handler.sendMessage(msg);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //异常处理
                        up.dismiss();
                        Log.e("onError", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        //监听完毕后的处理
                        Log.d("onComplete", "onComplete");
                    }
                });
    }


}
